<template>
	<view>
		<view v-for="(item, index) in list" :key="index" class="item" @click="itemClick(item)">
			<view class="info">
				<text>{{format(item.created)}}</text>
				<text>{{item.question}}</text>
				<text>提交人:{{item.creator}}</text>
			</view>
			<view class="action">
				<view class="button">
					<u-button class="btn" size="mini" @click="updateFAQSate(item.id, 2, index)">拒绝</u-button>
					<u-button class="btn" size="mini" @click="updateFAQSate(item.id, 1, index)">通过</u-button>
				</view>
				<text class="more">查看详情</text>
			</view>
		</view>
		<u-loadmore :status="status" />
	</view>
</template>

<script>
	import {
		getPageByState,
		updateById,
	} from '@/api/clientele.js'
	import { formatDateByTimestamp } from '@/utils/utils.js'
	export default {
		name: 'ApprovalFaq',
		data() {
			return {
			status: 'loadmore',
				page: -1,
				list: [],
			}
		},
		created() {
			this.getPage(this.page);
		},
		methods: {
			getPage(page) {
				getPageByState(page + 1, 30).then(res => {
					console.log(res);
					if (res.data.last) {
						this.status = 'nomore';
					}
					this.page = res.data.number;
					if (this.page === 0) {
						this.list = res.data.content;
					} else {
						this.list = this.list.concat(res.data.content);
					}
				})
			},
			
			updateFAQSate(id, state, position) {
				uni.showLoading({})
				updateById(id, state).then(res => {
					uni.hideLoading();
					this.$u.toast('操作成功');
					this.list.splice(position, 1);
				})
			},
			
			itemClick(item) {
				this.$u.route({
					url: 'pages/clientele/faq-detail-page',
					params: item,
				})
			},
			
			loadMore() {
				if (this.status === 'nomore') {
					return;
				}
				this.status = 'loading';
				this.getPage(this.page);
			},
			
			onReachBottom() {
				this.loadMore();
			},
			
			format(timestamp) {
				return formatDateByTimestamp(timestamp, 'yyyy年mm月dd日 hh时MM分')
			}
		}
	}
</script>

<style lang="scss">
	page {
		background-color: $app-page-bg-color;
	}

	.item {
		display: flex;
		flex-direction: row;
		justify-content: flex-start;
		align-items: center;
		padding: 20rpx;
		margin: 30rpx;
		background-color: #FFFFFF;
		border-radius: 20rpx;

		font-size: 24rpx;
		font-family: PingFang SC;
		font-weight: 400;
		color: $app-font-color;
		
		.info{
			display: flex;
			flex-direction: column;
			flex-grow: 1;
		}
		
		.action{
			display: flex;
			flex-direction: column;
			align-items: flex-end;
			.button{
				display: flex;
				flex-direction: row;
				
				.btn{
					margin-left: 10rpx;
				}
			}
			
			.more{
				margin-top: 20rpx;
			}
		}
	}
</style>
